import ElTreeGrid from 'element-tree-grid'
// var ElTreeGrid = require('element-tree-grid')
// Vue.component(ElTreeGrid.name,ElTreeGrid);
// 引入测试组件
import xxhTest from '@/components/xxh-test.vue'
export default {
  components: {
    [ElTreeGrid.name]: ElTreeGrid,
    xxhTest
  },
  data () {
    return {
      isNot: 'zhy',
      isLoading: false,
      // 获取商品数据列表 ---------------------
      pagenum: 1,
      pagesize: 6,
      total: 0,
      // 商品列表数据三级
      goodsList: [],
      // 对话框数据
      addGoodsForm: {
        // cat_pid 分类父 ID 不能为空
        // cat_name 分类名称 不能为空
        // cat_level 分类层级 不能为空
        cat_name: '',
        // cat_pid: -1,
        // cat_level: -1
        addGoodsSelected: []
      },
      // 商品列表二级
      goodsListLevel2: [],
      props: {
        label: 'cat_name',
        value: 'cat_id'
      },
      isShowAddGoodsDialog: false
    }
  },
  methods: {
    handleSunmit (value) {
      console.log(value)
    },
    /**
     * 获取商品列表数据
     * @param {number} pagenum 页码
     * @param {number} pagesize 每页的个数
     */
    async getGoodsList (pagenum = 1, pagesize = 6) {
      this.isLoading = true
      const res = await this.axios.get('categories', {
        params: {
          type: 3,
          pagenum,
          pagesize
        }
      })
      this.isLoading = false
      console.log(res)
      const { data: { data: { result, total }, meta: { status } } } = res
      if (status === 200) {
        this.goodsList = result
        this.total = total
      }
    },
    /**
     * 当页码改变时触发
     * @param {number} curPage 页码
     */
    pageChange (curPage) {
      this.pagenum = curPage
      this.getGoodsList(curPage)
    },

    /**
     * 点击添加按钮时触发
     */
    showAddGoodsDialog () {
      this.isShowAddGoodsDialog = true
    },
    /**
     * 点击确定添加分类
     */
    async addGoods () {
      const res = await this.axios.post('/categories/', {
        cat_pid: this.addGoodsForm.addGoodsSelected[this.addGoodsForm.addGoodsSelected.length - 1],
        cat_name: this.addGoodsForm.cat_name,
        cat_level: this.addGoodsForm.addGoodsSelected.length
      })
      const { data: { meta: { msg, status } } } = res
      if (status === 201) {
        this.$message({
          type: 'success',
          message: msg
        })
        this.isShowAddGoodsDialog = false
        this.getGoodsList()
        this.addGoodsForm.addGoodsSelected = []
        this.addGoodsForm.cat_name = ''
      } else {
        this.$message({
          type: 'error',
          message: msg
        })
      }
    }
  },
  async created () {
    this.getGoodsList()
    const res = await this.axios.get('categories', {
      params: {
        type: 2
      }
    })
    this.goodsListLevel2 = res.data.data
  }
}
